package cc.customer.center.Impl;

import cc.customer.center.WorkDiaklogServices;
import cc.customer.common.utils.Constant;
import cc.customer.common.utils.DateUtil;
import cc.customer.cusDynamic.domain.CusDynamic;
import cc.customer.cusDynamic.domain.EntityDiakogProjo;
import cc.customer.cusDynamic.services.CusDynamicService;
import cc.customer.myCustomer.damain.MyCustomer;
import cc.customer.myCustomer.services.PerSonServices;
import cc.customer.onschool.domain.OnSchoolResource;
import cc.customer.onschool.services.OnSchoolService;
import cc.customer.system.dao.UserMapper;
import cc.customer.system.domain.MyUser;
import cc.customer.system.service.UserService;
import cc.customer.utils.HttpRespons;
import cc.customer.utils.HttpUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Description: customer
 * Created by yhsh on 2019/11/8 11:02
 * version 2.0
 * 方法说明 销售管理 电话统计
 */
@Service
public class WorkDiaklogServicesImp  implements WorkDiaklogServices {

	private static final Logger logger = LoggerFactory.getLogger(WorkDiaklogServicesImp.class);

	@Autowired
	private OnSchoolService onSchoolService;
	@Autowired
	private PerSonServices perSonServices;
	@Autowired
	private CusDynamicService cusDynamicService;
	@Autowired
	private UserMapper userMapper;

	@Override
	public List<EntityDiakogProjo> dialogConnectStatic(EntityDiakogProjo entityDiakogProjo) {
		try {
			String starttime = "";
			String endtime  = "";
			if (StringUtils.isAnyEmpty(entityDiakogProjo.getStarttime(),entityDiakogProjo.getEndtime())){
				Date week = DateUtil.addDays(new Date(),-7);
				starttime = DateUtil.convert2String(week,"yyyy-MM-dd")+" 00:00:00";
				endtime = DateUtil.getCurrentDate("yyyy-MM-dd")+" 23:59:59";
			}else{
				starttime = entityDiakogProjo.getStarttime()+" 00:00:00";
				endtime = entityDiakogProjo.getEndtime()+" 23:59:59";
			}
			List<EntityDiakogProjo> list = new ArrayList<>();
			//获取姓名
			List<String> users = entityDiakogProjo.getDiaNames();
			List<MyUser> allUser = userMapper.selectAll();
			for (String user:users){
				logger.error("测试数据传输格式:"+user);
				/**获取分机号**/
				MyUser myUser = null;
				for (MyUser user1 : allUser){
					if (user1.getUserUuid().equals(user)){
						myUser = user1;break;
					}
				}
				if (null == myUser){ break; }
				Map<String ,String> maps = new HashMap<>();
				maps.put("src",myUser.getExtensionNumber());
				maps.put("startTime",starttime);
				maps.put("endTime",endtime);
				HttpRespons respons = HttpUtils.sendPost(Constant.FILE_RECORD_PATH,maps);
				Vector<String> vector =  respons.getContentCollection();
				if(!vector.isEmpty()){
					String message = vector.get(0);
					JSONObject obj = JSONObject.parseObject(message);
					JSONArray jsonArray = obj.getJSONArray("data");

					EntityDiakogProjo entity = null;
					SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.FULL_DATE_FORMAT);
					for (int i = 0; i<jsonArray.size(); i++){
						entity = new EntityDiakogProjo();
						entity.setDiaName(myUser.getNickname());
						JSONObject jsonObject = jsonArray.getJSONObject(i);
						entity.setNumber(jsonObject.getString("src")); //分机号
						entity.setDest(jsonObject.getString("dst"));  //目标电话
						entity.setDialogDruid(jsonObject.getString("billsec")); //时长
						entity.setDialogUrl(jsonObject.getString("url")); //录音文件地址
						String datedaule = jsonObject.getString("calldate");

						String formatDate =  sdf.format(new Date(Long.valueOf(datedaule)));
						entity.setDialogtime(formatDate);
						list.add(entity);
					}
				}
			}
			return list;
		}catch (Exception e){
			e.printStackTrace();
			logger.error("获取电话时长统计失败："+e.getMessage());
		}
		return new ArrayList<>();
	}
}
